package com.test.A01;

import java.lang.reflect.Field;
import java.util.Locale;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.Resource;

/*
  BeanFactory 与 ApplicationContext 的区别
 */
@SpringBootApplication
public class A01 {

	private static final Logger log = LoggerFactory.getLogger(A01.class);

	public static void main(String[] args) throws Exception {
		test6(args);
	}

	/***
	 * DefaultSingletonBeanRegistry单例类的用法
	 * 
	 * @param args
	 * @throws Exception
	 */
	private static void test1(String[] args) throws Exception {
		ConfigurableApplicationContext context = SpringApplication.run(A01.class, args);
		// DefaultSingletonBeanRegistry这个是得到单例对象 通过反射的方式得到属性
		Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");
		singletonObjects.setAccessible(true);
		// 得到bean工厂
		ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
		// 遍历，得到key和value
		Map<String, Object> map = (Map<String, Object>) singletonObjects.get(beanFactory);
		// 直接遍历得到所有的 bean
		map.forEach((k, v) -> {

			// System.out.println(k+"========================"+v);
		});

		// 得到特定的bean 以component开头的
		map.entrySet().stream().filter(e -> e.getKey().startsWith("component")).forEach(e -> {
			System.out.println(e.getKey() + "=" + e.getValue());
		});
	}

	/***
	 * MessageSource接口的用法 处理不用的消息的能力
	 * 
	 * @param args
	 */
	private static void test2(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(A01.class, args);
		System.out.println(context.getMessage("hi", null, Locale.CHINA));
		System.out.println(context.getMessage("hi", null, Locale.ENGLISH));
		System.out.println(context.getMessage("hi", null, Locale.JAPANESE));
	}
	
	/***
	   * ResourcePatternResolver  获取资源文件的能力
	   * @param args
	   * @throws Exception
	   */
	  public static void test3(String[] args) throws Exception {
		  ConfigurableApplicationContext context = SpringApplication.run(A01.class, args);
		  Resource[] resources = context.getResources("classpath:messages.properties");
	      for (Resource resource : resources) {
	           System.out.println(resource);
	      }
	      System.out.println("=================================================");
	      //classpath*这个还可以在jar中来找内容
	      Resource[] rs = context.getResources("classpath*:META-INF/spring.factories");
	      for (Resource resource : rs) {
	           System.out.println(resource);
	      }
	}
	  

	  /****
	   * ConfigurableApplicationContext  读取环境变量的能力
	   * @param args
	   */
	  private static void test4(String[] args) {
		  ConfigurableApplicationContext context = SpringApplication.run(A01.class, args);
		  ConfigurableEnvironment environment = context.getEnvironment();
		  System.out.println(environment.getProperty("java_home"));//可以得到电脑系统变量的能力
		  System.out.println(environment.getProperty("server.port"));//可以得到当前环境变量  配置文件里的
	}
	  
	  /***
	   * 发送事件，一个组件发送  另外一个组件接收  事件解耦作用
	   * @param args
	   */
	  private static void test6(String[] args) {
		  System.out.println("test6....");
		  ConfigurableApplicationContext context = SpringApplication.run(A01.class, args);
		  context.getBean(Component1.class).register();
		
	}
}
